<?php

namespace IDock\Insurance\DAJIA;

use IDock\IDock\IDockInterface;
use IDock\Insurance\DAJIA\Bean\Applicant;
use IDock\Insurance\DAJIA\Bean\BasePartReq;
use IDock\Insurance\DAJIA\Bean\BusinessInfo;
use IDock\Insurance\DAJIA\Bean\InsuredInfo;
use IDock\Insurance\DAJIA\Bean\VehicleInfo;
use IDock\Insurance\DAJIA\Bean\VehicleOwnerInfo;
use IDock\Insurance\DAJIA\Request\BusinessOrderNoQueryRequest;
use IDock\Insurance\DAJIA\Request\CancelOrderRequest;
use IDock\Insurance\DAJIA\Request\DeleteOrderRequest;
use IDock\Insurance\DAJIA\Request\ElectronicInsuranceDownloadRequest;
use IDock\Insurance\DAJIA\Request\ElectronicSignFileQueryRequest;
use IDock\Insurance\DAJIA\Request\ElectronicSignQueryRequest;
use IDock\Insurance\DAJIA\Request\InsuranceConfirmRequest;
use IDock\Insurance\DAJIA\Request\InsuranceOrderQueryRequest;
use IDock\Insurance\DAJIA\Request\InsuranceOrderTrackQueryRequest;
use IDock\Insurance\DAJIA\Request\InsurancePriceQueryRequest;
use IDock\Insurance\DAJIA\Request\NewCarRegisterRequest;
use IDock\Insurance\DAJIA\Request\NonCarOrderQueryRequest;
use IDock\Insurance\DAJIA\Request\NonCarUnderWritingRequest;
use IDock\Insurance\DAJIA\Request\OrderQueryRequest;
use IDock\Insurance\DAJIA\Request\RenewalInquiryRequest;
use IDock\Insurance\DAJIA\Request\TodayAuthCodeRequest;
use IDock\Insurance\DAJIA\Request\UnIndividualPaymentApplyRequest;
use IDock\Insurance\DAJIA\Request\UploadFileRequest;
use IDock\Insurance\DAJIA\Request\VehicleActualValueRequest;
use IDock\Insurance\DAJIA\Request\VehicleAndCustomerInfoConfirmRequest;
use IDock\Insurance\DAJIA\Request\VehicleModelRequest;
use IDock\Insurance\DAJIA\Response\CancelOrderResponse;
use IDock\Insurance\DAJIA\Response\CommonResponse;
use IDock\Insurance\DAJIA\Response\ElectricalInsuranceResponse;
use IDock\Insurance\DAJIA\Response\InsuranceConfirmResponse;
use IDock\Insurance\DAJIA\Response\InsuranceOrderQueryResponse;
use IDock\Insurance\DAJIA\Response\InsurancePriceQueryResponse;
use IDock\Insurance\DAJIA\Response\NonCarOrderQueryResponse;
use IDock\Insurance\DAJIA\Response\NonCarUnderWritingResponse;
use IDock\Insurance\DAJIA\Response\OrderQueryResponse;
use IDock\Insurance\DAJIA\Response\PolicyPaymentResponse;
use IDock\Insurance\DAJIA\Response\PreInquiryResponse;
use IDock\Insurance\DAJIA\Response\RenewalInquiryResponse;
use IDock\Insurance\DAJIA\Response\UploadFileResponse;
use IDock\Insurance\DAJIA\Response\VehicleActualValueResponse;
use IDock\Insurance\DAJIA\Response\VehicleModelResponse;
use IDock\Insurance\DAJIA\Response\NonCarProductListResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarPaymentVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 大家保险
 */
class DAJIAConstruct extends Builder implements IDockInterface
{

    protected $config = [];


    public function __construct($config)
    {
        $this->config = $config;

        $this->channelCode = $config['channelCode'] ?? '';
        $this->channelPwd = $config['channelPwd'] ?? '';
        $this->secretKey = $config['secretKey'] ?? '';
        $this->deviceType = $config['deviceType'] ?? '';
        $this->interCode = '';
        $this->transTime = date('Y-m-d H:i:s');
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor)
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);

        if ($response->printer and $response->printer->toArray()) {
            return  $response;
        } else {
            $request->switch = true;
            /*** @var VehicleModelResponse $response */
            $response = $this->execute($request);
        }

        return $response;
    }

    /**
     * 车辆实际价格
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): VehicleActualValueResponse
    {
        $request = new VehicleActualValueRequest();
        $request->setVisitor($vehicleActualValueVisitor);

        /*** @var VehicleActualValueResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumNoCarVisitor
     * @return PreInquiryResponse
     */
    public function preInquiry(PremiumInquiryVisitor $premiumNoCarVisitor)
    {
        $busiOpdpt = $premiumNoCarVisitor->getExtInfoVisitor()->argc['busiOpdpt'];
        $busiOprCde = $premiumNoCarVisitor->getExtInfoVisitor()->argc['busiOprCde'];
        $vhlType = $premiumNoCarVisitor->getExtInfoVisitor()->argc['vhlType'];
        $request = new BusinessOrderNoQueryRequest();
        $request->setBusiOpdpt($busiOpdpt);
        $request->setBusiOprCde($busiOprCde);
        $request->setVhlType($vhlType);

        /*** @var PreInquiryResponse $response */
        $response = $this->execute($request);
        $premiumNoCarVisitor->getExtInfoVisitor()->argc['orderNo'] = $response->printerInit()->printer->argc['orderNo'];
        $premiumNoCarVisitor->getExtInfoVisitor()->argc['busiId'] = $response->printerInit()->printer->argc['busiId'];
        //车辆信和客户信息确认接口
        $request = new VehicleAndCustomerInfoConfirmRequest();
        $request->setVisitor($premiumNoCarVisitor);
        $this->execute($request);

        return $response;
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        // TODO: Implement validateCar() method.
    }


    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return InsurancePriceQueryResponse
     * @throws \Exception
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        //保费计算
        $request = new InsurancePriceQueryRequest();
        $request->setVisitor($premiumInquiryVisitor);
        /**
         * @var InsurancePriceQueryResponse $response
         */
        $response = $this->execute($request);
        $response->translateCoverageCode($premiumInquiryVisitor->isNewEnergy);
        /**@var PremiumInquiryPrinter $printer */
        $printer = $response->getPrinter();

        //重复投保日期
        //if(empty($printer->repeatInsurePrinter['biStartData']) && !empty($printer->repeatInsurePrinter['ciStartData'])){
        //    $printer->repeatInsurePrinter['biStartData'] = $printer->repeatInsurePrinter['ciStartData'];
        //}
        //if(!empty($printer->repeatInsurePrinter['biStartData']) && empty($printer->repeatInsurePrinter['ciStartData'])){
        //    $printer->repeatInsurePrinter['ciStartData'] = $printer->repeatInsurePrinter['biStartData'];
        //}
        if ($printer && $response->isSuccess() && $printer->success) {
            $riskType = $request->getMotorJointType();
            if ($riskType == '1') {
                $renewMark = $response->jqRenewMrk;
            } else {
                $renewMark = $response->syRenewMrk;
            }
            $startDate = new \DateTime($premiumInquiryVisitor->getVehicleVisitor()->firstRegisterDate);
            $endDate = new \DateTime(date('Y-m-d H:i:s'));
            $days = $startDate->diff($endDate)->days;
            if ($renewMark === '0') {
                if ($days > 270) {
                    $value = '转保';
                } else {
                    $value = '新保';
                }
            } else {
                $value = '续保';
            }
            $printer->premiumFactorPrinter->ComFactorAppend('忠诚度', 'loyalMrk', $value);
            $printer->businessType = $value;
            //非车回显
            $nonCarInquiryPrinter = new NonCarInquiryPrinter();
            $totalPremium = 0;
            foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
                /*** @var $item NonCarProductVisitor */
                $single = new NonCarInquirySinglePrinter();
                $single->premium = $item->premium;
                $single->planCode = $item->planCode;
                $single->productCode = $item->productCode;
                $single->statusCode = 'success';
                $totalPremium += $single->premium * $item->insuredNum;
                $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
            }
            $nonCarInquiryPrinter->totalPremium = $totalPremium;
            $printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;
            $response->printer = $printer;
        }

        return $response;
    }

    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor)
    {

        //非车核保
        $request = new NonCarUnderWritingRequest();
        $nocarVisitor = $premiumInquiryVisitor->getNonCarUnderwritingVisitor();
        if($nocarVisitor->getNonCarList()){
            $nocarVisitor->argc['ciInquiryNo'] = $premiumInquiryVisitor->getUnderwritingVisitor()->ciInquiryNo;
            $nocarVisitor->argc['biInquiryNo'] = $premiumInquiryVisitor->getUnderwritingVisitor()->biInquiryNo;
            $request->setVisitor($nocarVisitor);
            $request->extVisitor = $premiumInquiryVisitor->getExtInfoVisitor();
//        global $testResponse;
//        $testResponse = '{"data":"24605DAFCAAACE9D3D1709A1A568DC2DCC1CFB6BBEF4E2C04CB5BA3145E32F2E36CA5976F3F74162A93D6EF1AC5CC5C4E537F36D4CD1570D74C51DC8337D11ED33CEDB74922F46DE410024A67D368FE3928C88B15CFD815C58E0752E1A2F6351A32D6A58AD8257BCF5D3B172CB47B168485EC1B004175657C717C938B767A9187D87AF379FA843073917F90095626CF806760524F0D0D66263EE0A2F27585B079405D400E4C6DEBD3ECAD4AEC5A66F202C6118DCA14107A455B47F6EBA6044B793D77773F5BF44ACEEAF0974DB3A0DC9A21A3C6634357DB222BB6B1B6E60BA0E1A2C0CEE35FCE49989FBB2C98BF383116FC14896DA463B30FD6E3E7F8B9BABC6F10A0AC40C6C8F0D11F8A3C99D1C7B95622947F12BDE0779928A009BA5C44AADCDCCD03FD6F2F193514BF01D0443CC9CEAC9FFA75CC7F0BA84E4884B7ABE68CE0AC6DB638085CDFAA75BDED2B8FF07D0F7EB66D94249C8F3505B9AF3C3CA561E978B2068FD4EA2B8146B95F659E1D3EA19DA4221E9F84A68BBA8D25768087D32F3CD4EFE7D261FF393531AEC00DFC31477EE8B9337D798B38DBC3C754629648D0F9B55B4001E0726C1BD288195BEE0FBC6D2A0C9A6BDDAC08391EBFE48C502A29FA2B77702C6D93855ABBD33FD89352CF077BD39A24CB70BC6FEADC0DDAEB9598B5714C58407498D01E8D36BD58C514994B29973D9558ED1A21DB2FE5E13B8FBC15D17A426A061A6B39114040D4656B5CBAFDEB8BB47E84A68D9353D0C1781416304BF25FE53C374B586053414DA3D865A2E714F79D7BDA0BD04123DEA2FDA051EDEAF11A310A5310C8380B40B0C81E29A680136926766E9BFC3E41F807FD26C7ED72CC06D23A3C33E39C06200E05FB4F4BF5AB258386897951F9F52B1B916612561D63E55118D5317C488D97CF34257F91428A4FFA2519F94A6CC2BE59CE7FEE743B8BB4E2E343A4FE24169863B3B1761C3C2FD0FBCED23C4C3B2A3608F1330850CF441C32E5837C35165649F6DBEA0498B959A87A53D06DC8D9BABB67672780663788E5D016FBF53A8ED8190A2A890CC736D8BE23A662F7243055EC48DF05C2873E94D2EC1BC73F2B990580912AA9CB6CEC0950A17CB7BF3B866109CEA44CF","signature":"a582b864abee65c10071d61eec67c766","timestamp":"1702549587059"}';
            /**
             * @var NonCarUnderWritingResponse $noCarResponse
             */
            $noCarResponse = $this->execute($request);

            if(!$noCarResponse->isSuccess()){
                return $noCarResponse;
            }
        }


        $request = new InsuranceConfirmRequest();
        $request->setVisitor($premiumInquiryVisitor);
//        $testResponse = '';
        /**
         * @var InsuranceConfirmResponse $response
         */
        $response = $this->execute($request);
        /*** @var PremiumUnderwritingPrinter $printer */
        $printer = $response->getPrinter();
        if($response->isSuccess() && $printer && isset($noCarResponse)){
            //非车回填
            $printer->noCarUnderwritingPrinter = $noCarResponse->printer;
        }
        return $response;
    }

    public function policyPayment(PolicyPaymentVisitor $paymentVisitor)
    {
        //获取电子签名接口
        $request = new ElectronicSignQueryRequest();
        $request->setVisitor($paymentVisitor);

        /*** @var PolicyPaymentResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function uploadFile(UploadFileVisitor $uploadFileVisitor)
    {
        $request = new UploadFileRequest();
        $request->setVisitor($uploadFileVisitor);
        $imageList = $uploadFileVisitor->getFileList();
        $uploadFileVisitor->fileList = [];
        if (empty($imageList)) {
            throw new \Exception("上传图片不能为空");
        }
        $response = '';
        foreach ($imageList as $val) {
            $upVisitor = new UploadFileVisitor();
            $upVisitor->argc = $uploadFileVisitor->argc;
            $upVisitor->ciInquiryNo = $uploadFileVisitor->ciInquiryNo;
            $upVisitor->biInquiryNo = $uploadFileVisitor->biInquiryNo;
            $upVisitor->inquiryNo = $uploadFileVisitor->inquiryNo;
            $upVisitor->fileAppend($val['type'], $val['url'], $val['base64']);
            $request->setVisitor($upVisitor);
            /*** @var UploadFileResponse $response */
            $response = $this->execute($request);
        }
        return $response;
    }

    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor)
    {
        $request = new OrderQueryRequest();
        $request->setVisitor($proposalStatusVisitor);

        /*** @var OrderQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function proposalRecall(ProposalRecallVisitor $revokeVisitor)
    {
        $request = new CancelOrderRequest();
        $request->setVisitor($revokeVisitor);

        /**
         * @var CancelOrderResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor)
    {
        //电子保单下载
        $request = new ElectronicInsuranceDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /**
         * @var ElectricalInsuranceResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor)
    {
        $request = new CancelOrderRequest();
        $request->setVisitor($recallVisitor);

        /**
         * @var CancelOrderResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor)
    {
        $request = new OrderQueryRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var OrderQueryResponse $response */
        $response = $this->execute($request);
        return $response;
    }
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------
    //--------------------------------


    /**
     * 保险订单查询
     * @param $orderNo
     * @return InsuranceOrderQueryResponse
     */
    public function InsuranceOrderQuery($orderNo)
    {
        $request = new InsuranceOrderQueryRequest();
        $request->setOrderNo($orderNo);

        /**
         * @var InsuranceOrderQueryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * @param $orderNo
     * @return CommonResponse
     */
    public function TodayAuthCode($orderNo)
    {
        $request = new TodayAuthCodeRequest();
        $request->setOrderNo($orderNo);
        $request->setDate(date('Y-m-d'));

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function InsuranceOrderTrackQuery($sys, $beginDate, $endDate)
    {
        $request = new InsuranceOrderTrackQueryRequest();
        $request->setSysRes($sys);
        $request->setBeginDate($beginDate);
        $request->setEndDate($endDate);

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function BusinessOrderNoQuery($busiOpdpt, $busiOprCde, $vhlType)
    {
        $request = new BusinessOrderNoQueryRequest();
        $request->setBusiOpdpt($busiOpdpt);
        $request->setBusiOprCde($busiOprCde);
        $request->setVhlType($vhlType);

        /**
         * @var PreInquiryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function CarActualValueQuery($orderNo, $prodNo, $effectiveDate)
    {
        $request = new VehicleActualValueRequest();

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;

    }


    public function DeleteOrder($orderNo)
    {
        $request = new DeleteOrderRequest();
        $request->setOrderNo($orderNo);

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function UnIndividualPaymentApply($orderNo)
    {
        $request = new UnIndividualPaymentApplyRequest();
        $request->setOrderNo($orderNo);

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    //public function UploadFile($orderNo, $syProposalNo, $jqProposalNo, $appCode, $dataId, $appType, $orgNum, $oprId, $upType, $imageList = [])
    //{
    //    $request = new UploadFileRequest();
    //    $request->setOrderNo($orderNo);
    //    $request->setSyProposalNo($syProposalNo);
    //    $request->setJqProposalNo($jqProposalNo);
    //    $request->setAppCode($appCode);
    //    $request->setDataId($dataId);
    //    $request->setAppType($appType);
    //    $request->setOrgNum($orgNum);
    //    $request->setOprId($oprId);
    //    $request->setUpType($upType);
    //    $request->setImageList($imageList);
    //
    //    /**
    //     * @var CommonResponse $response
    //     */
    //    $response = $this->execute($request);
    //    return $response;
    //}

    public function OrderTrackQueryByOrderNo($orderNo)
    {
        $request = new OrderQueryRequest();
        $request->setOrderNo($orderNo);

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function ElectronicSignFileQuery($proposalNo)
    {
        $request = new ElectronicSignFileQueryRequest();
        $request->setProposalNo($proposalNo);

        /**
         * @var CommonResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor)
    {
        $response = new NonCarProductListResponse();
        $response->seat = $nonCarProductListVisitor->seats;
        $response->printerInit();
        $response->setCode(200);
        return $response;
    }


    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
        // TODO: Implement nonCarInquiry() method.
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {

    }

    /**
     * 非车订单查询
     * @param NonCarPaymentVisitor $nonCarPaymentVisitor
     * @return NonCarOrderQueryResponse
     */
    public function nonCarOrderQuery(NonCarPaymentVisitor $nonCarPaymentVisitor)
    {
        $request = new NonCarOrderQueryRequest();
        $request->setVisitor($nonCarPaymentVisitor);

        /**
         * @var NonCarOrderQueryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }
}
